/**
 * Created by bgy on 2016/12/6.
 * 我的水果
 */
import React from 'react';
import {Link,hashHistory} from 'react-router';
import GoodsListCompent from './goodsList.js';
import GoodsDetailComponent from './goodsDetail.js';
import GoodsQualityComponent from './goodsQuality.js';
//import GoodsApplyQualityComponent from './goodsApplyQuality.js';
import AppFQIComponent from './fruit-qt/appFQI.js';
import GoodsQualityRecordComponent from './qualityRecord.js';
import GoodsQualityRecordPageComponent from './qualityRecordPage.js';

class HeadTitleComponent extends React.Component {

  handleClick(e) {
    e.preventDefault();
    switch (this.props.handle) {
      case '发布水果':
        window.history.back();
        break;
      default:
        this.props.handleBack();
    }

  }

  handleFruit() {
    switch(this.props.handle){
      case'发布水果':
            console.log('发布水果界面');
            //window.location.href='publishFruit';
            hashHistory.push('publishFruit');
            break;
      case'编辑':
        console.log('编辑水果界面');
        this.props.editFruit();
        break;
      case'保存':
        console.log('保存水果界面');
        this.props.saveFruit();
        break;
    }
    //this.props.handleFruit();
  }

  render() {
    return (
      <div className="fruit-header">
        <a onClick={this.handleClick.bind(this)} className='leftBtn'>&nbsp;</a>
        <span>{this.props.title}</span>
        <span onClick={this.handleFruit.bind(this)} className='fruit-publish'>{this.props.handle}</span>
      </div>
    )
  }
}

class MyFruits extends React.Component {
  handleBack() {
    switch(this.state.title){
      case '质检报告':
        if(this.state.subTitle){
          this.setState({
            showDetailBox:false,
            applyQualityTestBox: false,
            showQualityBox: false,
            qualityRecordBox: true,
            qualityRecordPage: false,
            subTitle:false,
            showListBox: false
          });
        }else{
          this.setState({
            showDetailBox:false,
            applyQualityTestBox: false,
            showQualityBox: false,
            qualityRecordBox: false,
            qualityRecordPage: false,
            subTitle:false,
            showListBox: true,
            goodsId: ' ',
            title: '我的水果',
            handle: '发布水果'
          });
        }
         break;
      default:
        this.setState({
          showDetailBox:false,
          applyQualityTestBox: false,
          showQualityBox: false,
          qualityRecordBox: false,
          qualityRecordPage: false,
          subTitle:false,
          showListBox: true,
          goodsId: ' ',
          title: '我的水果',
          handle: '发布水果'
        });
    }
  }

  showDetail(goodsId) {
    console.log('goodId=='+goodsId);
    if (goodsId) {
      this.setState({goodsId: goodsId});
      setTimeout(function () {
        this.setState({goodsId: goodsId});
      }.bind(this), 500);
    }
    this.setState({
      showDetailBox: true,
      handle: '编辑'
    });
  }

  showQuality(status, vendorGoodsId) {
    this.setState({
      vendorGoodsId: vendorGoodsId,
      handle: ''
    });
    setTimeout(function () {
      this.setState({vendorGoodsId: vendorGoodsId});
    }.bind(this), 500);
    switch (status) {
      case 'A':   //质检通过-查看质检报告
      case 'C':    //未通过质量检测-查看质检报告
      case 'D':    //已提交质检申请-查看质检申请
        this.setState({
          title: '质检报告',
          showListBox: false,
          showDetailBox: false,
          applyQualityTestBox: false,
          showQualityBox: false,
          qualityRecordPage: false,
          qualityRecordBox: true,
          subTitle:false
        });
        break;
      case 'B':   //尚未申请质检-申请质检
        this.setState({
          title: '百果园水果质检标准',
          showListBox: false,
          showDetailBox: false,
          applyQualityTestBox: false,
          showQualityBox: true,
          qualityRecordBox: false,
          qualityRecordPage: false,
          subTitle:false
        });
        break;
    }
  }

  applyQualityTest(vendorGoodsId) {
    this.setState({
      vendorGoodsId: vendorGoodsId,
      title:'申请质检',
      showListBox: false,
      showDetailBox: false,
      showQualityBox: false,
      applyQualityTestBox: true,
      qualityRecordBox: false,
      qualityRecordPage: false,
      subTitle:false
    });
    setTimeout(function(){
      this.setState({
        vendorGoodsId: vendorGoodsId
      })
    }.bind(this),500);
  }

  changeTitle(){
    this.setState({
      title:'选择水果',
      handle:' ',
    })
  }

  handleCheckQuality(recordItem,recordStatus){
    console.info('查看质结果'+recordStatus);
    console.info(recordItem);
    //根据recordStatus判断跳往哪个页面，状态为AC跳往查看质检报告页面，状态为DEF跳往查看申请/申请质检界面
    switch(recordStatus){
      case 'A':
      case 'C':
            console.log('查看质检报告界面');
            this.setState({
              recordStatus:recordStatus,
              title:'质检报告',
              subTitle:true,
              showListBox: false,
              showDetailBox: false,
              showQualityBox: false,
              applyQualityTestBox: false,
              qualityRecordBox: false,
              qualityRecordPage: true
            });
          setTimeout(function () {
            this.setState({recordStatus: recordStatus});
          }.bind(this), 500);
      case 'D':
      case 'E':
      case 'F':
            console.log('查看质检申请界面');
            break;
    }
  }

  componentDidMount() {
    //this.setState({
    //  myGoods: this.state.myFruitsTestData
    //})
  }
  editFruit(){
    this.setState({
      handle:' ',
      edit:true
    });
  }
  saveFruit(){
    this.setState({
      handle:' ',
      edit:false
    });
  }
  gobackFruit(){
    this.setState({
      title:'我的水果',
      showListBox: true,
      showDetailBox: false,
      showQualityBox: false,
      applyQualityTestBox: false,
      qualityRecordBox: false,
      qualityRecordPage: false,
      subTitle:false
    });
    window.location.reload(true);
  }

  constructor() {
    super();
    this.state = {
      showListBox: true,
      showDetailBox: false,
      showQualityBox: false,
      applyQualityTestBox: false,
      qualityRecordBox: false,
      goodsId: ' ',
      recordStatus:' ',
      vendorGoodsId: ' ',
      title: '我的水果',
      handle: '发布水果',
      subTitle:false,
      edit:false
    };
  }

  render() {

    return (
      <article>
        <HeadTitleComponent title={this.state.title} handle={this.state.handle}
                            handleBack={this.handleBack.bind(this)} editFruit={this.editFruit.bind(this)} saveFruit={this.saveFruit.bind(this)}/>
        <article className={this.state.showListBox?'fruit-detail':'fruit-detail sink'}>
          <section >
            <GoodsListCompent showDetail={this.showDetail.bind(this)} showQuality={this.showQuality.bind(this)}/>
          </section>
        </article>
        <article className={this.state.showDetailBox?'fruit-detail':'fruit-detail sink'}>
          <section className="fruit-box">
            <GoodsDetailComponent showDetail={this.showDetail.bind(this)} gobackFruit={this.gobackFruit.bind(this)} data-goodsId={this.state.goodsId} data-edit={this.state.edit} />
          </section>
        </article>
        <article className={this.state.showQualityBox?'fruit-detail':'fruit-detail sink'}>
          <section className="fruit-box">
            <GoodsQualityComponent showQuality={this.showQuality.bind(this)}
                                   applyQualityTest={this.applyQualityTest.bind(this)}
                                   data-vendorGoodsId={this.state.vendorGoodsId}/>
          </section>
        </article>
        <article className={this.state.applyQualityTestBox?'fruit-detail':'fruit-detail sink'}>
          <section className="fruit-box">
            <AppFQIComponent />
          </section>
        </article>
        <article className={this.state.qualityRecordBox?'fruit-detail':'fruit-detail sink'}>
          <section className="fruit-box">
            <GoodsQualityRecordComponent data-vendorGoodsId={this.state.vendorGoodsId} handleCheckQuality={this.handleCheckQuality.bind(this)}/>
          </section>
        </article>
        <article className={this.state.qualityRecordPage?'fruit-detail':'fruit-detail sink'}>
          <section className="fruit-box">
            <GoodsQualityRecordPageComponent data-recordStatus={this.state.recordStatus}/>
          </section>
        </article>
      </article>

    );
  }
}
export default MyFruits;
